package com.atkid.boot.controller;


import cn.yueshutong.springbootstartercurrentlimiting.annotation.CurrentLimiter;
import com.atkid.boot.entity.Club;
import com.atkid.boot.entity.Manager;
import com.atkid.boot.entity.vo.StuVo;
import com.atkid.boot.result.Result;
import com.atkid.boot.service.ClubService;
import com.atkid.boot.service.ManagerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author kid
 * @since 2021-12-07
 */
@RestController
@RequestMapping("/boot/manager")
public class ManagerController {

  @Autowired
  ManagerService managerService;
  @Autowired
  ClubService clubService;

  @GetMapping("/{current}/{size}/{cid}")
  public Result getStudents(@PathVariable Long current, @PathVariable Long size,@PathVariable int cid) {
    Page<StuVo> res = managerService.getStudents(current,size,cid);
    return Result.ok().data("student",res);
  }

  @GetMapping("/{id}")
  public Result getClubByManager(@PathVariable int id) {
    Club club = managerService.getClubByManager(id);
    return Result.ok().data("club",club);
  }

  @PostMapping("/login")
  public Result login(@RequestBody Manager manager) {
    String name = manager.getName();
    QueryWrapper<Manager> wrapper = new QueryWrapper<>();
    wrapper.eq("name",name);
    Manager m = managerService.getOne(wrapper);
    if (m == null || !m.getPassword().equals(manager.getPassword())) {
      return Result.error().message("用户名或密码错误");
    }
    Integer clubId = m.getClubId();
    if (clubId == -1) {
      return Result.ok().code(30000);
    }
    Club club = clubService.getById(clubId);
    return Result.ok().data("club",club);
  }
}

